import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { ArticleLike } from 'src/article-like/entities/article-like.entity';
import { ArticleView } from 'src/article-view/entities/article-view.entity';
import { Comment } from 'src/comment/entities/comment.entity';
import { User } from 'src/user/entities/user.entity';

export class Article {
  id: number; // 文章ID
  title: string; // 标题
  content: string; // 文章内容
  userId: number; // 外键用户ID
  createdAt: Date; // 创建时间
  updatedAt: Date; // 修改时间
  isDeleted: boolean; // 是否删除
  comments: Comment[]; // 评论

  @ApiProperty({
    type: 'number',
  })
  @Transform(({ value }: { value: ArticleLike[] }) => {
    return value.length;
  })
  likes: ArticleLike[]; // 喜欢
  user: User; // 用户

  @ApiProperty({
    type: 'number',
  })
  @Transform(({ value }: { value: ArticleView[] }) => {
    return value.length;
  })
  views: ArticleView[]; // 浏览量
}

export const ArticleInclude = {
  user: {
    include: {
      avatarFile: true,
    },
  },
  likes: true,
  comments: {
    include: {
      user: {
        include: {
          avatarFile: true,
        },
      },
    },
  },
  views: true,
};
